﻿using AutoMapper;
using BasicData.API.Read.Applications.Commands;
using BasicData.API.Read.Dtos;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infrastrueturc.Interfaces;
using MediatR;

namespace BasicData.API.Read.Applications.CommandHandlers
{
    /// <summary>
    /// 地址请求
    /// </summary>
    public class AddressQueryCommandHandler : IRequestHandler<AddressQueryCommand, APIResult<List<AddressDTO>>>
    {
        private readonly IBaseReposiotry<Address> addressRepsoitory;
        private readonly IMapper mapper;
        public AddressQueryCommandHandler(IBaseReposiotry<Address> addressRepsoitory, IMapper mapper)
        {
            this.addressRepsoitory = addressRepsoitory;
            this.mapper = mapper;
        }

        public Task<APIResult<List<AddressDTO>>> Handle(AddressQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<AddressDTO>> result = new APIResult<List<AddressDTO>>();
            result.Code = ResultCode.查询成功;
            result.Message = ResultCode.查询成功.ToString();
            var list = addressRepsoitory.GetAll().ToList();
            var query = mapper.Map<List<AddressDTO>>(list);
            result.Data = query;
            return Task.FromResult(result);
        }
    }
}
